package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RequestMapping("/cargo/extCproduct")
@Controller
@Slf4j
public class ExtCprodurctController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    /**
     * 列表
     * @param contractId
     * @param contractProductId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public String list(String contractId , String contractProductId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        log.info("执行货物的附件列表查询开始...");
        //1、查询附件
         ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
         PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample, pageNum, pageSize);

         //2、查询工厂
         FactoryExample factoryExample = new FactoryExample();
         factoryExample.createCriteria().andCtypeEqualTo("货物");
         List<Factory> factoryList = factoryService.findAll(factoryExample);

        //3、保存
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("factoryList",factoryList);
        //修改，删除需要
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }

    /**
     * 增加或者修改
     * @param extCproduct
     * @return
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct){
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());
        //id判断是添加还是修改
        if (StringUtils.isEmpty(extCproduct.getId())) {
            extCproductService.save(extCproduct);
        } else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list?contractId="+extCproduct.getContractId()
                +"&contractProductId="+extCproduct.getContractProductId();
    }

    //进入修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(String contractId,String ContractProductId,String id){
        //查询厂家
         FactoryExample factoryExample = new FactoryExample();
         factoryExample.createCriteria().andCtypeEqualTo("附件");
         List<Factory> factoryList = factoryService.findAll(factoryExample);

        //查询附件进行回显
         ExtCproduct extCproduct = extCproductService.findById(id);

        //保存
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("extCproduct",extCproduct);
        // extc-update.jsp隐藏域需要使用
        request.setAttribute("contractId",contractId);
        request.setAttribute("ContractProductId",ContractProductId);
        return "cargo/extc/extc-update";
    }

    @RequestMapping("/delete")
    public String delete(String id, String contractId ,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list?contractId="+contractId
                +"&contractProductId="+contractProductId;
    }


}
